import { asyncRoutes } from '@/router';
export function convertRouter(routers) {
  let array = [];
  for (let i in routers) {
    for (let s in asyncRoutes) {
      if (routers[i].path == asyncRoutes[s].path) {
        array.push({ ...asyncRoutes[s] });
        if (s != 0) array[s].children = [];
        for (let o in routers[i].children) {
          for (let p in asyncRoutes[s].children) {
            if (
              routers[i].children[o].path == asyncRoutes[s].children[p].path &&
              routers[i].children[o].path != '/index'
            ) {
              array[s].children.push(asyncRoutes[s].children[p]);
            }
          }
        }
      }
    }
  }
  console.log(array, 123);
  return array;
}
/**
 * @description 处理后端路由数据
 * @param {*} route 路由数据
 * @param {*} list 前端路由 asyncRoutes配置项
 * @returns {*}
 */

function hasPermission(permissions, route) {
  if (route.meta && route.meta.permissions) {
    return permissions.some((role) => route.meta.permissions.includes(role));
  } else {
    return true;
  }
}

export function filterAsyncRoutes(routes, permissions) {
  const finallyRoutes = [];
  routes.forEach((route) => {
    const item = { ...route };
    if (hasPermission(permissions, item)) {
      if (item.children) {
        item.children = filterAsyncRoutes(item.children, permissions);
      }
      finallyRoutes.push(item);
    }
  });
  return finallyRoutes;
}
